//
//  XTabBarController.m
//  XWeibo-OC
//
//  Created by Apple on 15/10/25.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "XTabBarController.h"
#import "XHomeController.h"
#import "XDescoverController.h"
#import "XMeController.h"
#import "XMessageController.h"

@interface XTabBarController ()



@end

@implementation XTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.tintColor = [UIColor orangeColor];
    
    //首页
    XHomeController *homeVC = [[XHomeController alloc] init];
    [self addChildController:homeVC titile:@"首页" normalImage:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
    
    //消息
    XMessageController *messageVC = [[XMessageController alloc] init];
    [self addChildController:messageVC titile:@"消息" normalImage:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
    
    //占位按钮
    [self addChildController:[[UIViewController alloc] init] titile:@"" normalImage:@"d" selectedImage:@"dd"];
    
    //发现
    XDescoverController *descoverVC = [[XDescoverController alloc] init];
    [self addChildController:descoverVC titile:@"发现" normalImage:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
    
    //我
    XMeController *meVC = [[XMeController alloc] init];
    [self addChildController:meVC titile:@"我" normalImage:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
    
    
    //设置导航栏标题颜色
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    navBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
    
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    
    attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:98/255.0 green:98/250.0 blue:98/255.0 alpha:1.0];
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:23];
    
    [navBar setTitleTextAttributes:attributes];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    CGFloat width = self.tabBar.bounds.size.width / 5;
    CGFloat height = self.tabBar.bounds.size.height;
    
    //创建加号按钮
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(width * 2, 0, width, height)];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    [btn addTarget:self action:@selector(composeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tabBar addSubview:btn];
}

#pragma mark - 按钮点击事件
- (void)composeBtnClick
{
    NSLog(@"----------------");
}

//给tabBarControll添加子控制器
- (void)addChildController:(UIViewController *)childController titile:(NSString *)title normalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage
{
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childController];

    //标题
    
    [self addChildViewController:nav];
    
    nav.tabBarItem.title = title;
    
    nav.tabBarItem.image = [UIImage imageNamed:normalImage];
    
    //选中状态图片
    UIImage *selImage = [UIImage imageNamed:selectedImage];
    selImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selImage;
}





@end
